#include "sqlist.h"

void test1()
{
    list *s = NULL;
    ListHeadInsert(&s,2);
    ListHeadInsert(&s,3);
    ListHeadInsert(&s,4);
    ListHeadInsert(&s,5);
    ListHeadInsert(&s,6);
    ListPrint(s);
}

void test2()
{
    list *s = NULL;

    ListTailInset(&s,2);
    ListTailInset(&s,3);
//    ListTailInset(&s,4);
//    ListTailInset(&s,5);
//    ListTailInset(&s,6);
    ListPrint(s);
    ListTaildel(&s);
    ListPrint(s);
    ListTaildel(&s);
    ListPrint(s);
    ListTaildel(&s);
    ListPrint(s);

}
void test3()
{
    list *s = NULL;
    ListHeadInsert(&s,2);
    ListHeadInsert(&s,3);
    ListHeadInsert(&s,4);
    ListHeadInsert(&s,5);
    ListHeadInsert(&s,6);
    ListTailInset(&s,2);
    ListTailInset(&s,3);
    ListTailInset(&s,4);
    ListTailInset(&s,5);
    ListTailInset(&s,6);
    ListPrint(s);
}
int main()
{
    test2();

    return 0;
}

void ListPrint(list* phead)
{
    list* cur = phead;
    while (cur)
    {
        printf("%d-> ",cur->data);
        cur = cur->next;
    }
    printf("\n");
}
list* CreatNode(listtype data)
{
    list* newnode = (list*)malloc(sizeof(list));
    if(newnode == NULL)
    {
        printf("malloc fail\n");
        exit(-1);
    }
    newnode->data = data;
    newnode->next = NULL;
    return newnode;
}
void ListTailInset(list** phead, listtype x)
{
    list* newnode = CreatNode(x);
//    assert(phead);
//创建一个新的node
    if(*phead == NULL)
    {
        *phead = newnode;
    }
    else
    {
        list* cur = *phead;
        //尾部插入
        while (cur->next)
        {
            cur = cur->next;
        }
        cur->next = newnode;
    }
}

void ListHeadInsert(list** phead, listtype x)
{   //创建new node
    list* newnode = CreatNode(x);
    newnode->next = *phead;
    *phead = newnode;
}

void ListHeaddel(list** phead)
{
    assert(*phead);
    list* cur = *phead;
    *phead = (*phead)->next;
    free(cur);
    cur = NULL;
}

void ListTaildel(list** phead)
{   list* cur = *phead;
    list* pre = *phead;
    assert(*phead);
    if((*phead)->next == NULL)
    {
        free(*phead);
        *phead = NULL;
        printf("CAN NOT");
    }
    else
    {
        while(cur->next !=NULL)
        {   pre = cur;
            cur = cur->next;
        }
        pre->next = NULL;
        free(cur);
        cur = NULL;
    }
}

